<?php

class UserController extends MyClass_Action {
	protected $controller;
	protected $action;
	protected $_user;


	public function init() {

		$this->commonAction();	

	}
	//用户首页
	public function indexAction() {
		try {
			 
			 
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}
	
		
		//用户首页
	public function opAction() {
		try {
			if (! $this->isPost ()) {
					throw new Exception ( '参数错误！' );
			}
			
			$p =$this->_request->getPost ();
			$isok=0;
			foreach($p['username'] as $key => $value){
				if($key==1)
					continue;
					
				$data=array('username'=>$value);
				if(!empty($p['password'][$key]))
					$data['password']=md5($p['password'][$key]);
				
				if(!empty($p['actionlist'][$key]))
					$data['action_list']=implode(',',$p['actionlist'][$key]);
					
				$result=$this->_dbAdapter ->update ( 'users',$data,'id='.$key );
				
				if($result)
					$isok=1;	
			}
			if($isok)
				$this->feedback ( '更新成功', '注意', '/'.$this->controller, 'warning' );
			else	
				throw new Exception ( '更新失败 ！' );
			//print_r($p);exit;
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	
		//用户首页
	public function changepasswordAction() {
		try {
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}

		//用户首页
	public function opchangepasswordAction() {
		try {
			if (! $this->isPost ()) {
					throw new Exception ( '参数错误！' );
			}
			
			$p =$this->_request->getPost ();
			if (empty ( $p['oldpassword'] )) {
				throw new Exception ( '旧密码不能为空 ！' );
			}
			
			if (empty ( $p['newpassword'])) {
				throw new Exception ( '新密码不能为空！' );
			}
			
			if ($p['repassword'] != $p['repassword']) {
				throw new Exception ( '两次密码不一致！' );
			}
			$p['oldpassword']=md5($p['oldpassword']);
			$sql = 'select password from users where id=' .$this->_user ->id;
			$password=$this->_dbAdapter ->fetchOne ( $sql );
			
			if($password !=$p['oldpassword'])
				throw new Exception ( '旧密码错误 ！' );
			
			$result=$this->_dbAdapter ->update ('users',array('password'=>md5($p['newpassword'])),'id='. $this->_user ->id);
			if($result)
				$this->feedback ( '更新成功', '注意', '/login/out', 'warning' );
			else 
				throw new Exception ( '更新失败 ！' );	

		 	
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}
	//查询家长对应的孩子  	
	public function haveparentsAction() {
		try {
		$id = ( int ) $this->_request->getParam ( 'id' );
		$page = ( int ) $this->_request->getParam ( 'page' );	
		$this->view->page=$page;	  
		if (empty($id)) {
				throw new Exception ( '参数错误！' );
			}
		$sql="select * from group_student_class_parents_v  where pid='".$id."' ";
		$student_unit=$this->_dbAdapter ->fetchAll($sql); 
		 //print_r($result);exit;
		$sql="select * from parents where pid='".$id."'";
		$parents=$this->_dbAdapter ->fetchRow($sql);  
		$parents['password']= $this->authcode($parents['password'], 'DECODE', 'mactop') ;
		$this->view->parents=$parents;	  
		//查询孩子所在的机构信息
/*		$student_uid=array();
		 foreach($result as $key => $value){
			 $student_uid[]=$value['uid'];
			 }
		//  print_r($student_uid);
	    $struid = implode(',',$student_uid);
		 
		if($struid){ 
		    $sql='select  * from  group_school_class_student_v  where studentid  in ('.$struid.') '; 
		    $student_unit=$this->_dbAdapter ->fetchAll($sql);  
		 }
*/		 
		$this->view->result=$result;	 
		$this->view->student_unit=$result;	
		//$this->view->student_unit=$student_unit;	
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	 
	
	//用户首页
	public function addAction() {
		try { 
			//自动生成账号
			$username=rand(100000,999999);
			
		    $password= rand(100000,999999);	
			
			$sql="select pid from parents where username='".$username."' ";
			$pnum = $this->_dbAdapter->fetchOne ($sql);
			 
			if(empty($pnum)){
			   $this->view->username=$username;	
			   $this->view->password=$password;	 
			   }else{
		     $this->feedback ( '生成账号中请稍后', '注意', '/'.$this->controller.'/add', 'warning',2 );  	   
				   }
			  
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}
  //处理增加账号信息	
	public function addopAction() {
		try {
			 //处理学生信息
			if (! $this->isPost ()) {
					throw new Exception ( '参数错误！' );
			}
			
			$p =$this->_request->getPost ();
			 // print_r($p);  exit;
			if(empty($p['realname'])){
				throw new Exception ( '家长姓名不能为空！' );				
				} 
			if(empty($p['mobile'])){
				throw new Exception ( '手机号不能为空！' );				
				}
			if(!preg_match('|^[0-9]{11}$|',$p['mobile'])){
					throw new Exception ('手机号输入错误');
			   } 
			 
			if(empty($p['cnname'])){
				throw new Exception ( '学生姓名不能为空，请确认！' );				
				}else{
				foreach($p['cnname'] as  $key => $value){
					
					if(empty($value)){
						throw new Exception ( '学生姓名不能a为空！' );	
					 } 
				  }	
			  } 
				
			if(empty($p['birthday'])){
				throw new Exception ( '请选择生日！' );				
				}else{
					foreach($p['birthday'] as  $key => $value){
					if(empty($value)){
						throw new Exception ( '生日不能为空！' );	
					 } 
				  }
			   }	
		  
		
			   
			$isok=0;
		  //家长信息
		
		  $sql="select * from parents where mobile='".$p['mobile']."'";
		  $parentOne=$this->_dbAdapter ->fetchRow ($sql);
		  
		  if($parentOne){
			$lastid= $parentOne['pid'];
		  }else{			  
			   $data=array();
			   $data['realname'] =$p['realname'];
			   $data['mobile'] =$p['mobile'];
			   $data['username'] =$p['username'];
			   $data['password'] =$this->authcode($p['password'],'','mactop');
			   //查询表中是否添加过此家长 
				
			   $parents=$this->_dbAdapter ->insert ('parents',$data); 
			   $lastid=  $this->_dbAdapter->lastInsertId ();   
		   } 
		 
		   if($lastid){
			   $isok=1;
		   }else{
			$this->feedback ( '添加家长失败', '注意', 'javascript:window.history.back();', 'warning' );     
			   } 
		  if($isok)	{
			  $datastudent= array();
			  foreach($p['cnname'] as $key =>$value){
				   $datastudent['cnname']= $value;
				   $datastudent['nikename']= $value;
				   $datastudent['birthday']=$p['birthday'][$key];  
				   $datastudent['sex']=$p['sex'.$key];
				   $datastudent['pid']=$lastid;  
				  $parents=$this->_dbAdapter ->insert ('student',$datastudent);  
				  } 
		  if(!empty($p['id'])){
			  
			 $this->feedback ( '添加学生成功', '成功', '/'.$this->controller.'/haveparents/id/'.$p['id'].'/page/'.$p['page'], 'tip' );     
			}else{ 
			$this->feedback ( '添加学生成功', '成功', 'javascript:window.history.back();', 'tip' );  	   
		   }
		} 
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	//查询所有的家长信息
	
   public function parentsAction() {
		try { 
		    
		    $where='where 1';	
			 
		   $keywolrd =  $this->_request->getParam ( 'text' ); 
		   $this->view->keywolrd=$keywolrd; 
			
			
		    if($keywolrd){
			  $where.=' and  username like "%'.$keywolrd.'%" or mobile like "%'.$keywolrd.'%" or realname  like "%'.$keywolrd.'%"  ';	
			   } 
			$count = 20;
			$curpage = ( int ) $this->_request->getParam ( 'page' );
	     	if (! $curpage)
    				$curpage = 1;
			$this->view->pageNum=$curpage;		
    			$offset = ($curpage - 1) * $count; 
			 $sql="select SQL_CALC_FOUND_ROWS *  from parents  ".$where."   order by pid ASC limit $offset,$count";
			//echo $sql;  exit;
			$result = $this->_dbAdapter->fetchAll ($sql);  
			  foreach($result as $key => $value){
				  
			 	  $result[$key]['password']= $this->authcode($value['password'], 'DECODE', 'mactop') ;
				                          
		       }  
		    	$this->view->result= $result;
			 
    			$param ['file'] = '/' . $this->controller . '/' . $this->action;
    			$param ['totalnum'] = $this->_dbAdapter->fetchOne ( 'SELECT FOUND_ROWS()' ); 
    			$param ['perpagenum'] = $count; // 每页显示的数目
    			$param ['disnum'] = 11; // 取单数显示，当前页停在中间
    			$param ['curpage'] = $curpage; // 当前页码
				$this->view->allnum= $param ['totalnum'];

    			//	require_once 'MyClass/class.page.php';
    			$page = new MyClass_Page ( $param );
    		//	$page->setvar ( array ('year' => $year,'week'=>$week,'foodtimeid'=>$foodtimeid) );
				$this->view->page= $page->getNumPage ();
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
  
  //绑定孩子操作
   public function bindstudentAction() {
		try {  
		if (! $this->isPost ()) {
			throw new Exception ( '参数错误！' );
			}
        $p =$this->_request->getPost (); 
       //  print_r($p); exit;
	    $groupid= substr($p['ma'],0,3);  
	      //连接运营库
        //$ydb=$this->getDbLink($groupid);
	     //判断绑定码
	    $sql="select class_uid,studentid from student where bindcode='".$p['ma']."' ";
		$class_uid=$this->_dbAdapter->fetchRow($sql); 
		 
		if($class_uid){ 
		  $result = $ydb->update ('student',array('uid'=>$p['student_uid']),'bindcode="'.$p['ma'].'"');
			  if($result){
				 $data=array();
				 $time=time();
				 $data['studentid']=$p['student_uid'];
				 $data['uid_class']=$class_uid['class_uid'];
				 $data['studentid']=$class_uid['studentid'];
				 $data['addtime']=$time;  
				 $class_student= $this->_dbAdapter->insert ('student_class',$data);
				    if($class_student){
					 $this->feedback ( '恭喜，绑定成功', 'note', '/'.$this->controller.'/haveparents/id/'.$p['pid'], 'tip',3 );                         }else{
						$this->feedback ( '绑定失败 ', 'note', 'javascript:window.history.back();', 'warning' ); 
						 }  
				} else{
					 $this->feedback ( '绑定失败 ', 'note', 'javascript:window.history.back();', 'warning' );  
					  }
		   }else{ 
				$this->feedback ( '绑定失败 ', 'note', 'javascript:window.history.back();', 'warning' ); 
				}
		 
	  //   }  
	} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	//解除绑定 
	public function unbindcodeAction() {
	  try { 
	     
	   $student_uid = ( int ) $this->_request->getParam ( 'student_uid' );
	   $uid_class = ( int ) $this->_request->getParam ( 'uid_class' );
	   $dbname =  $this->_request->getParam ( 'enname' ); 
       $pid = ( int ) $this->_request->getParam ( 'pid' ); 
	   
	   $sql="select * from groups where dbname='".$dbname."'"; 
	    $groupsres = $this->_dbAdapter->fetchRow ($sql);  
		
	   if(empty($student_uid)){
		   throw new Exception ( '参数错误！' );
		   }
	   if(empty($uid_class)){
		   throw new Exception ( '参数错误1！' );
		   }
		if(empty($dbname)){
		   throw new Exception ( '参数错误2！' );
		   }   	   
	   //删除 student_class 中的记录
	   $unstudent_class=$this->_dbAdapter->delete('student_class','studentid="'.$student_uid.'" and uid_class="'.$uid_class.'"');
	   if($unstudent_class){
		  //连接运营库
		  //$ydb=$this->getDbLink($groupname_en=$groupsres['groupid']);
		  
		  $class_uid=$this->_dbAdapter->update('student',array('uid'=>0),'uid="'.$student_uid.'" and class_uid="'.$uid_class.'"');          if($class_uid){ 
		 	$this->feedback ( '解除绑定成功', 'note', '/'.$this->controller.'/haveparents/id/'.$pid, 'tip',3 );  
			  }else{
				$this->feedback ( '解除绑定失败', 'note', 'javascript:window.history.back();', 'warning' );   
				  } 
	     }else{ 
			$this->feedback ( '操作失败', 'note', 'javascript:window.history.back();', 'warning' );   
		  }
	    
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	
	//查询学生列表	
	 public function studentAction() {
		try {  	
				  
		    $where='where  1=1';	
			 
		   $groupid = ( int ) $this->_request->getParam ( 'groups' );
		   $school_id = $this->_request->getParam ( 'school' ); 		
	       $classid  = $this->_request->getParam ( 'class' ); 
		   $cnname  = $this->_request->getParam ( 'cnname' ); 
		    
		  $this->view->groups=$groupid; 
		  $this->view->school_id=$school_id;  
		  $this->view->classid=$classid; 
		  $this->view->cnname=$cnname; 
		  
				  if($school_id){ 
					  $schoolarray=explode('_',$school_id);  
					  $schoolid=$schoolarray['0'];
				  }
				   
				 if($groupid){
					  $where .=' and  a.groupid="'.$groupid.'" ';
					 $this->view->groupid= $groupid; 
					 }
					 
				 if($schoolid){
					 $where_school=' and schoolid='.$schoolid;
					$where .=' and  a.schoolid="'.$schoolid.'" '; 
					$this->view->schoolid= $schoolid; 
					} 
					
				 if($classid){
					$where .=' and  a.classid="'.$classid.'" ';
					  $this->view->classid= $classid; 
					 }	
					 
				 if($cnname){
					$where.=' and a.cnname="'.trim($cnname).'" ';
					$this->view->cnname= $cnname; 				 
					} 
			  
			$count = 20;
			$curpage = ( int ) $this->_request->getParam ( 'page' );
	     	if (! $curpage)
    				$curpage = 1;
			  $this->view->pageNum= $curpage;			
					
    			$offset = ($curpage - 1) * $count; 
			    $sql="select SQL_CALC_FOUND_ROWS a.*,b.isopen from group_student_class_parents_v a left join chunyu_parents b on a.pid=b.pid ".$where." and a.inactive=0  order by pid ASC limit $offset,$count";
			    // echo $sql;  exit;
			    $result = $this->_dbAdapter->fetchAll ($sql);  
			   
		    	$this->view->result= $result;
			  
    			$param ['file'] = '/' . $this->controller . '/' . $this->action;
    			$param ['totalnum'] = $this->_dbAdapter->fetchOne ( 'SELECT FOUND_ROWS()' ); 
    			$param ['perpagenum'] = $count; // 每页显示的数目
    			$param ['disnum'] = 11; // 取单数显示，当前页停在中间
    			$param ['curpage'] = $curpage; // 当前页码
				$this->view->allnum= $param ['totalnum'];

    			//	require_once 'MyClass/class.page.php';
    			$page = new MyClass_Page ( $param );
    			$page->setvar ( array ('groups' => $groupid,'school'=>$schoolid,'class'=>$classid) );
				$this->view->page= $page->getNumPage ();
				
				$sql="select schoolid,start_time,end_time,isopen_chunyu,`month` from service_school_order where isdelete=0 and productid=1 and from_unixtime(start_time,'%Y-%m-%d')<'".date('Y-m-d')."' and from_unixtime(end_time,'%Y-%m-%d')>'".date('Y-m-d')."' ".$where_school;
				//echo $sql;
				$service = $this->_dbAdapter->fetchAssoc ($sql);
				$this->view->service= $service;
				 //print_r($service);print_r($result);exit;
				
				
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	//查询学生对应的家长信息，单个的信息
	
	 public function oneparentsAction() {
		try { 
	   //获取传递的pid  查询详细信息
	  // echo "aaaaaaaaa";  exit;
	    $groupid = ( int ) $this->_request->getParam ( 'groups' );
	    $schoolid = $this->_request->getParam ( 'school' ); 		
	    $classid  = $this->_request->getParam ( 'class' ); 
	    $page  = $this->_request->getParam ( 'page' ); 
	    $id  = $this->_request->getParam ( 'id' ); 
	    $cnname  = $this->_request->getParam ( 'cnname' ); 
	      
	  $this->view->groups=$groupid; 
	  $this->view->school=$schoolid;  
	  $this->view->classid=$classid; 
	  $this->view->page=$page; 
	  $this->view->id=$id; 
	  $this->view->cnname=$cnname; 
	  
	  
	  $sql="select * from  parents where pid='".$id."'  ";
	  $result = $this->_dbAdapter->fetchRow ($sql); 
	  
	   
	  $result['password']= $this->authcode($result['password'], 'DECODE', 'mactop') ;
		 
	  $this->view->result=$result; 
	
	} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	
	//班级账号列表
	 public function classAction() {
		try { 
		    $groupid = ( int ) $this->_request->getParam ( 'groups' );
		    $schoolid = $this->_request->getParam ( 'school' ); 		
	        $classid  = $this->_request->getParam ( 'class' ); 
		    
		  $this->view->groups=$groupid; 
		  $this->view->school=$schoolid;  
		  $this->view->classid=$classid; 
		 
		    $where='where 1=1 ';	 
		    
			 // $groupid=$p['select1']; 
			  if($schoolid){ 
				  $schoolarray=explode('_',$schoolid);  
				  $schoolid=$schoolarray['0'];
			  } 
			 if($groupid){
				  $where .=' and  groupid="'.$groupid.'" ';
				  $this->view->groupid= $groupid; 
				 }
			 if($schoolid){
			    $where .=' and  schoolid="'.$schoolid.'" '; 
				$this->view->schoolid= $schoolid; 
			    } 
			 if($classid){
			    $where .=' and  classid="'.$classid.'" ';
				  $this->view->classid= $classid; 
				 }
			  
			  
		     $count = 20;
			 $curpage = ( int ) $this->_request->getParam ( 'page' ); 
	     	if (! $curpage)
    				$curpage = 1;
			     $this->view->pageNum= $curpage;		
    			$offset = ($curpage - 1) * $count; 
			    $sql="select SQL_CALC_FOUND_ROWS *  from teacher_class_duty_v  ".$where." and inactive=0   limit $offset,$count";
			    // echo $sql;  exit;
			    $result = $this->_dbAdapter->fetchAll ($sql);  
			     foreach($result as $key => $value){
				  
			 	  $result[$key]['password']= $this->authcode($value['password'], 'DECODE', 'mactop') ;
				                          
		       } 
			    
		    	$this->view->result= $result;
			 
    			$param ['file'] = '/' . $this->controller . '/' . $this->action;
    			$param ['totalnum'] = $this->_dbAdapter->fetchOne ( 'SELECT FOUND_ROWS()' ); 
    			$param ['perpagenum'] = $count; // 每页显示的数目
    			$param ['disnum'] = 11; // 取单数显示，当前页停在中间
    			$param ['curpage'] = $curpage; // 当前页码
				$this->view->allnum= $param ['totalnum'];

    			//	require_once 'MyClass/class.page.php';
    			$page = new MyClass_Page ( $param );
    			$page->setvar ( array ('groups' => $groupid,'school'=>$schoolid,'class'=>$classid) );
				$this->view->page= $page->getNumPage (); 
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
    
	//删除信息
      public function deleteAction() {
		try { 
         $id = ( int ) $this->_request->getParam ( 'id' );		
         $type =  $this->_request->getParam ( 'type' );	
		 $keyworld="";	
         if($type=='parents'){
			 $keyworld.="pid";
		  }else if($type=='class'){ 
			  $keyworld.="uid"; 
	      }else if($type=='student'){
			  $keyworld.="uid"; 				 
		  }  
       $del_result = $this->_dbAdapter->delete ( $type, $keyworld.'='.$id);
	     
	   if($del_result){ 
	      //throw new Exception ( '删除成功！' );	  
		 $this->feedback ( '删除成功', '成功', '/user/'.$type.'/', 'tip','2' );
		   }
	     
	} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
  }
	//deletestunew 删除 学生信息
	
	 public function deletestunewAction() {
		try { 
	   
	     $stuid = ( int ) $this->_request->getParam ( 'stuid' );	 
	     $cid = ( int ) $this->_request->getParam ( 'cid' );
		 $groups = ( int ) $this->_request->getParam ( 'groupid' );
		 $school = $this->_request->getParam ( 'schoolid' );
		 $class = ( int ) $this->_request->getParam ( 'classid' );
		 $pageNum = ( int ) $this->_request->getParam ( 'pageNum' );
		 
		//根据学生的uid 删除 student 中的学生
		$isok=0;
		 $del_result = $this->_dbAdapter->delete ('student', 'uid='.$stuid);
		if($del_result){
			 $result = $this->_dbAdapter->delete ('student_class', 'studentid='.$stuid.' and  uid_class='.$cid);
			  if($result){
				  $isok=1;
				  }
			} 
	      if($isok){ 
			    
			 $this->feedback ( '删除成功', '成功', '/user/student/page/'.$pageNum.'/groups/'.$groups.'/school/'.$school.'/class/'.$class, 'tip','2' );
			   }else{
				 $this->feedback ( '操作失败！', 'note', 'javascript:window.history.back();', 'warning' );   
				   } 
	 
	 
	  } catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
  }
	
	
	
	//删除学生信息
      public function deletestudentAction() {
		try { 
         $uid = ( int ) $this->_request->getParam ( 'uid' ); 
		 $pid = ( int ) $this->_request->getParam ( 'pid' ); 
         
	     //查询学生是有机构信息
		 if($uid){
		 	$sql='select  * from  group_school_class_student_v  where studentid='.$uid; 
		    $student_unit=$this->_dbAdapter ->fetchAll($sql);  
			
			if($student_unit){
			  $this->feedback ( '请解除绑定以后，在进行删除！', 'note', 'javascript:window.history.back();', 'warning' ); 
				}else{
				   $del_result = $this->_dbAdapter->delete ('student', 'uid='.$uid);
				    if($del_result){ 
	     				 //throw new Exception ( '删除成功！' );	  
						 $this->feedback ( '删除成功', '成功', '/user/haveparents/id/'.$pid, 'tip','2' );
						   }else{
							 $this->feedback ( '操作失败！', 'note', 'javascript:window.history.back();', 'warning' );   
							   }  
				  }
		      }
	    
	     
	} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
  }
	
	
	 
	  
	//修改家长信息
      public function editparentsAction() {
		try { 
         $id = ( int ) $this->_request->getParam ( 'id' );	
		 $page = ( int ) $this->_request->getParam ( 'pageNum' );
		 $this->view->page= $page; 
		 
		 $sql="select * from parents where pid ='".$id."' ";
 		 $result = $this->_dbAdapter->fetchRow ( $sql);
		 
	     $result['password']= $this->authcode($result['password'], 'DECODE', 'mactop') ;
		 
        $this->view->result= $result;
	} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	//修改学生信息
	 public function editstudentAction() {
		try { 
         $student_uid = ( int ) $this->_request->getParam ( 'id' );	
		 $page = ( int ) $this->_request->getParam ( 'pageNum' ); 
		 $groupid = ( int ) $this->_request->getParam ( 'groupid' ); 
		 $schoolid =  $this->_request->getParam ( 'schoolid' ); 
		 $classid =  $this->_request->getParam ( 'classid' ); 
		 $cnname =  $this->_request->getParam ( 'cnname' );
			$this->view->page= $page;
			$this->view->groupid= $groupid;
			$this->view->schoolid= $schoolid;
			$this->view->classid= $classid;
		    $this->view->cnname= $cnname;
		 //查询学生的信息
		 $sql="select * from group_student_class_parents_v where studentid='".$student_uid."' ";
		 $result = $this->_dbAdapter->fetchRow ( $sql);
	 
		  //查询学生所在的班级信息
		 //
		//  print_r($result);
		
		
		
         $this->view->result= $result;
	} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	//
	 
	 public function opstudentAction() {
		try {
		if (! $this->isPost ()) {
					throw new Exception ( '参数错误！' );
			}
			
			$p =$this->_request->getPost ();
			//print_r($p);exit;
		    $page = ( int ) $this->_request->getParam ( 'pageNum' );
		  
/*		    $sql="select * from groups where groupname_en='".$p['groupname_en']."'";
		    $groupsres = $this->_dbAdapter->fetchRow ($sql);  
*/		 
			//更新线上的表student
		  $data=array(
		  	'cnname'=>$p['cnname'],
		  	'nikename'=>$p['nikename'],
		  	'birthday'=>$p['birthday'],
		  	'sex'=>$p['sex'],
		  );
		  $result = $this->_dbAdapter->update ('student',$data,'studentid='.$p['uid_student']);  
		  //$isok=''; 
		  if($result){
			   $this->feedback ( '修改成功','成功','/user/student/pageNum/'.$page.'/groups/'.$p['groupid'].'/class/'.$p['classid'].'/cnname/'.$p['cnname'],'tip',3 );   
		  }else{
		     $this->feedback ( '修改失败或者已经被修改,请查看！', 'note', 'javascript:window.history.back();', 'warning',3 );  		
		  }
/*		if($isok){
			//连接运营库 
			//$db=$this->getDbLink($groupname_en=$groupsres['groupid']) ;
			// $data2=array();
			
			 $res= $this->_dbAdapter->update ('student',array('cnname'=>$p['cnname'],'enname'=>$p['nikename']),'uid='.$p['studentid']); 
			 if($result){
			    	 $this->feedback ( '修改成功','成功','/user/student/page/'.$page.'/groups/'.$p['groupid'].'/school/'.$p['schoolid'].'/class/'.$p['classid'].'/cnname/'.$p['cnname'],'tip',3 ); */  
/*			      }else{
					 $this->feedback ( '修改失败或者已经被修改！', 'note', 'javascript:window.history.back();', 'warning',3 ); 			                  }
			 }	*/
		 
	} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
   
   //重置密码
   
    public function resetpasswordAction() {
		try { 
         $id = ( int ) $this->_request->getParam ( 'id' ); 
		 $data=array();
		 $data['password']=$this->authcode('111111', '', 'mactop') ; 
 		 $result = $this->_dbAdapter->update ('parents',$data,'pid='.$id);
		 if($result){ 
		    $this->feedback ( '修改成功','成功','/user/editparents/id/'.$id,'tip',3 ); 
		  }else{ 
			 $this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' ); 	 
				 } 
          
	} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
 //重置班级密码
 public function repasswordAction() {
		try { 
         $teacherid = ( int ) $this->_request->getParam ( 'teacherid' );	 
		 $page = ( int ) $this->_request->getParam ( 'pageNum' );	 
		 $name =  $this->_request->getParam ( 'name' );	 
		 $classid =  $this->_request->getParam ( 'classid' );	 

		 $groups=$this->_dbAdapter->fetchOne('select groupid from groups where groupname_en="'.$name.'"');
		 $school=$this->_dbAdapter->fetchRow('select uid,schoolid from class where classid="'.$classid.'"');
		 $data=array();
		 $data['password']=$this->authcode('111111', '', 'mactop') ; 
 		 $result = $this->_dbAdapter->update ('teacher',$data,'teacherid='.$teacherid);
        
			  if($result){
				  $this->feedback ( '修改成功','成功','/user/class/groups/'.$groups.'/school/'.$school['schoolid'].'/class/'.$school['uid'].'/page/'.$page,'tip',3 );  
			  }else{
				  $this->feedback ( '修改失败', 'note', 'javascript:window.history.back();', 'warning' ); 	  
			  }
		  
	} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
   	
	

   public function opparentsAction() {
		try { 
         if (! $this->isPost ()) {
					throw new Exception ( '参数错误！' );
			}			
		 $p =$this->_request->getPost ();  
		
		 $page = ( int ) $this->_request->getParam ( 'page' );	 
		
		
		 // $sql="select * from parents where mobile='".$p['mobile']."'";
		 // $parentOne=$this->_dbAdapter ->fetchRow ($sql);
		  
		 // if($parentOne){
		//		throw new Exception ( '该手机号的家长已存在，不能修改！' );		 
		 // }  
		 
	    $data=array();
		$data['username']=$p['username'] ;
		$data['password']=$this->authcode($p['password'], '', 'mactop') ;
		$data['realname']=$p['realname'] ; 
		$data['mobile']=$p['mobile'] ; 
		$data['canpull']=$p['canpull'] ; 
		 
	    $result = $this->_dbAdapter->update ('parents',$data,'pid='.$p['pid']);
		
		if($result){ 
		  $this->feedback ( '修改成功', '注意', '/'.$this->controller.'/parents/page/'.$page, 'tip' );	
			}else{
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );	
				}
	 
       
	} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}

   


   public function groupsAction() {
		try { 
         $sql="select * from  groups ";
	     $result = $this->_dbAdapter->fetchAll ($sql);
		 for($i = 0; $i<count($result); $i++)
		{
	      $str .= "<option value='".$result[$i]['groupid']."'>".$result[$i]['groupname']."</option>";
		}
		  
	    echo $str;  exit;
	} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	//获取所有的校园列表
	  public function schoollistAction() {
		try { 
           if (! $this->isPost ()) {
					throw new Exception ( '参数错误！' );
			}			
		  $p =$this->_request->getPost ();  
		 
		  $sql="SELECT * FROM `groups` where groupid='".$p['id']."' ";
		  $result = $this->_dbAdapter->fetchRow ($sql);
	 
		    $sql="select * from  school where groupid=".$p['id'];   
			$schoollist= $this->_dbAdapter->fetchAll ($sql);
	                 

			 
		 for($i = 0; $i<count($schoollist); $i++)
		{
		  $str .= "<option value='".$schoollist[$i]['schoolid'].'_'.$result['groupid']."'>".$schoollist[$i]['schoolname']."</option>";
		}
		   
	      echo $str;  exit;
	} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	 
	
	//获取所有的班级信息
	  public function classlistAction() {
		try { 
           if (! $this->isPost ()) {
					throw new Exception ( '参数错误！' );
			}			
		  $p =$this->_request->getPost ();  
		  $array=explode('_',$p['id']);
		  
		  $sql="SELECT * FROM `groups` where groupid='".$array['1']."' ";
		  $result = $this->_dbAdapter->fetchRow ($sql);
	 
		  $sql="select * from  class  where isdel=0 and schoolid='".$array['0']."' ";   
		  $classlist= $this->_dbAdapter->fetchAll ($sql);
	  
			 
		 for($i = 0; $i<count($classlist); $i++)
		{
		  $str .= "<option value='".$classlist[$i]['classid']."'>".$classlist[$i]['classname']."</option>";
		}
		   
	      echo $str;  exit;
	} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	
	
	
	
	 //以下内容保持不变
	public function noRouteAction() {
		$this->_redirect ( '/' );
	}
	
	public function __call($method, $args) {
		if ('Action' == substr ( $method, - 6 )) {
			$url = '/' . $this->controller . '/index';
			return $this->_redirect ( $url );
		}
		throw new Exception ( 'Invalid method' );
	}}

